package com.cobla.struct.stack;

import lombok.Data;

/**
 * 链栈 node
 */
@Data
public class LinkedStack {

    private int data;
    private LinkedStack next;

    public LinkedStack() {}

    public LinkedStack(int data, LinkedStack next) {
        this.data = data;
        this.next = next;
    }

    public LinkedStack(int data) {
        this.data = data;
        this.next = null;
    }

    /**
     * 栈空
     * @return
     */
    public int isEmpty() {
        if (this.next == null) {
            return 1;
        }
        return 0;
    }

    /**
     * 入栈
     * @param x
     */
    public void push(int x) {
        this.next = new LinkedStack(x, this.next);
    }

    /**
     * 出栈
     * @return 成功返回 元素，失败返回 null
     */
    public Integer pop() {
        if (this.next == null) {
            return null;
        }
        LinkedStack popElem = this.next;
        this.next = this.next.next;
        return popElem.data;
    }


}
